﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using BLL = MS.InformationManagement.BLL;
using MS.InformationManagement.DAL.RecordCenter;

namespace MS.InformationManagement.Presenter.TRDSelector
{
    public class TRDSelectorPresenter: BasePresenter<ITRDSelectorVIew>
    {

        #region fields
       
        #endregion

        #region Constructors

        public TRDSelectorPresenter(ITRDSelectorVIew view) : base(view) { } 

        #endregion

        #region Public Methods

        public void SetUserDependency(string sectionName) {
            BLL.TRDSelector.TRDSelector businessObject = new BLL.TRDSelector.TRDSelector();
            Seccion section = businessObject.GetSeccion(sectionName);
            if (section.SecciónPadre == null | !section.SecciónPadre.Id.HasValue | section.SecciónPadre.Id.Value == 0)
            {
                View.SelectedSection = section.Id.Value;
                HandleLoadSubSeccion();
            }
            else {
                View.SelectedSection = section.SecciónPadre.Id.Value;
                HandleLoadSubSeccion();
                View.SelectedSubSection = section.Id.Value;
                View.LoadSubSeccion();
            }
            View.LoadSelectValues();
        }

        public void SetCurrentUserDependency()
        {
            BLL.TRDSelector.TRDSelector businessObject = new BLL.TRDSelector.TRDSelector();
            Seccion section = businessObject.GetSeccionForCurrentUser();
            if (section.SecciónPadre == null | !section.SecciónPadre.Id.HasValue | section.SecciónPadre.Id.Value == 0)
            {
                View.SelectedSection = section.Id.Value;
                HandleLoadSubSeccion();
            }
            else
            {
                View.SelectedSection = section.SecciónPadre.Id.Value;
                HandleLoadSubSeccion();
                View.SelectedSubSection = section.Id.Value;
                View.LoadSubSeccion();
            }
            View.LoadSelectValues();
        }

        public void HandleLoad()
        {
            if ((View.SelectedSubSection.HasValue && View.SelectedSubSection.Value != 0) || (View.SelectedSubSerie.HasValue && View.SelectedSubSerie != 0))
            {
                LoadInitialValues();
            }
            else
            {
                LoadSeriesSeccion();
                View.Show();
            }
        }

        public void HandleLoadSubSeccion()
        {
            BLL.TRDSelector.TRDSelector businessObject = new BLL.TRDSelector.TRDSelector();
            View.SubSecciones = businessObject.GetSubSecciones(View.SelectedSection);
            View.LoadSubSeccion();
        }

        public void HandleLoadSubSeries()
        {
            BLL.TRDSelector.TRDSelector businessObject = new BLL.TRDSelector.TRDSelector();
            View.SubSerie = businessObject.GetSubSeries(View.SelectedSerie);
            View.LoadSubSerie();
        }

        public void LoadInitialValues()
        {

            BLL.TRDSelector.TRDSelector bllObject = new BLL.TRDSelector.TRDSelector();
            LoadSeriesSeccion();
            if (View.SelectedSubSection.HasValue && View.SelectedSubSection != 0)
            {
                View.SubSecciones = bllObject.GetAllBrotherSubSections(View.SelectedSubSection.Value);
                if (View.SubSecciones != null && View.SubSecciones.Count() > 0)
                    View.SelectedSection = View.SubSecciones.ElementAt(0).SecciónPadre.Id.Value;
            }

            if (View.SelectedSubSerie.HasValue && View.SelectedSubSerie != 0)
            {
                View.SubSerie = bllObject.GetAllBrotherSubSeries(View.SelectedSubSerie.Value);
                if (View.SubSerie.Count() > 0)
                    View.SelectedSerie = View.SubSerie.ElementAt(0).Serie.Id.Value;
            }

            if (View.SelectedSection != 0 && View.SelectedSubSection.HasValue && View.SelectedSerie != 0 && View.SelectedSubSerie.HasValue) {
                View.TiposDocumentales = bllObject.GetDocumentalTypes(View.SelectedSection, View.SelectedSubSection.Value, View.SelectedSerie, View.SelectedSubSerie.Value);
                if (View.TiposDocumentales != null && View.TiposDocumentales.Count() > 0)
                    View.SelectedTipoDocumentalID = View.TiposDocumentales.ElementAt(0).Id.Value;
            }
            
            View.Show();
            View.LoadSelectValues();
        }

        private void LoadSeriesSeccion()
        {
            BLL.TRDSelector.TRDSelector bllObject = new BLL.TRDSelector.TRDSelector();
            View.Secciones = bllObject.GetSecciones();
            View.Serie = bllObject.GetSeries();
        }

        public void LoadTiposDocumentales()
        { 
            BLL.TRDSelector.TRDSelector bllObject = new BLL.TRDSelector.TRDSelector();
            View.TiposDocumentales = bllObject.GetDocumentalTypes(View.SelectedSection, View.SelectedSubSection.Value, View.SelectedSerie, View.SelectedSubSerie.Value);
        }

        public void CheckCurrentTRD() {
            if (View.SelectedTipoDocumentalID.HasValue) {
                BLL.TRDSelector.TRDSelector bllObject = new BLL.TRDSelector.TRDSelector();
                View.BelongsToCurrentTRD =  bllObject.BelongsToCurrentTRD(View.SelectedSection, View.SelectedSubSection.Value, View.SelectedSerie, View.SelectedSubSerie.Value, View.SelectedTipoDocumentalID.Value);
            }
           
            
        }

        #endregion

    }
}
